/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StudentPanel.java
 *
 * Created on Mar 24, 2011, 2:41:34 PM
 */

package courseScheduler;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mike
 */
public class StudentPanel extends SpecialContentPanel {

    /** Creates new form StudentPanel */
    public StudentPanel(MainThread newParent) {
        parentThread = newParent;
        System.out.println("here");
        getPrefixes();
        System.out.println("there");
        initComponents();
        makeAddSQL(0);
        getSections();
        fillDropList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListAdd = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        PrefixCombo = new javax.swing.JComboBox();
        NumberCombo = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ListView = new javax.swing.JList();
        btnDrop = new javax.swing.JButton();
        btnPrint = new javax.swing.JButton();
        btnMore = new javax.swing.JButton();
        btnExit2 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(700, 400));
        setPreferredSize(new java.awt.Dimension(400, 300));

        jTabbedPane1.setPreferredSize(new java.awt.Dimension(300, 300));

        jLabel1.setText("Prefix:");

        jLabel2.setText("Number:");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnClear.setText("Clear");

        btnExit.setText("Exit");

        jScrollPane1.setViewportView(ListAdd);

        jLabel3.setText("Courses:");

        PrefixCombo.setModel(new javax.swing.DefaultComboBoxModel(coursePrefixes));
        PrefixCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PrefixComboActionPerformed(evt);
            }
        });

        NumberCombo.setEnabled(false);
        NumberCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NumberComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                            .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(PrefixCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NumberCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(66, 66, 66))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(PrefixCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(NumberCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnClear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnExit))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Add", jPanel1);

        jLabel4.setText("Courses:");

        jScrollPane2.setViewportView(ListView);

        btnDrop.setLabel("Drop");
        btnDrop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDropActionPerformed(evt);
            }
        });

        btnPrint.setLabel("Print");

        btnMore.setLabel("More Info");

        btnExit2.setLabel("Exit");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnMore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExit2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnPrint, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDrop))
                .addContainerGap(93, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(57, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnExit2)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(btnDrop)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnPrint)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnMore)))
                .addGap(49, 49, 49))
        );

        jTabbedPane1.addTab("View", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addGap(24, 24, 24))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnDropActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDropActionPerformed
        System.out.println(ListView.getSelectedIndex());
        try{
            int selected = ListView.getSelectedIndex();
            System.out.println(ListView.getSelectedIndex());
            System.out.println(sectiondropid[selected]);
            System.out.println("DELETE FROM student_section WHERE section_id = " + sectiondropid[ListView.getSelectedIndex()] + " AND student_id = " + parentThread.getID() + ";");
            parentThread.getConn().createStatement().executeUpdate("DELETE FROM student_section WHERE section_id = " + sectiondropid[ListView.getSelectedIndex()] + " AND student_id = " + parentThread.getID() + ";");
        }
        catch(Exception ex){
            System.out.println(ex.getCause());
        }
        fillDropList();
    }//GEN-LAST:event_btnDropActionPerformed

    private void PrefixComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PrefixComboActionPerformed
        System.out.println(PrefixCombo.getSelectedIndex());
        if(PrefixCombo.getSelectedIndex() == 0){
            NumberCombo.setEnabled(false);
            makeAddSQL(0);
            getSections();
        }
        else{
            getNumbers();
            NumberCombo.setEnabled(true);
            NumberCombo.setModel(new javax.swing.DefaultComboBoxModel(courseNumber));
            NumberCombo.setSelectedIndex(0);
            addSQL = new String();
            makeAddSQL(1);
            System.out.println(addSQL);
            getSections();
        }
    }//GEN-LAST:event_PrefixComboActionPerformed

    private void NumberComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NumberComboActionPerformed
        if (NumberCombo.getSelectedIndex() == 0){
            makeAddSQL(1);
        }
        else{
            makeAddSQL(2);
        }
        getSections();
    }//GEN-LAST:event_NumberComboActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        addClass();
    }//GEN-LAST:event_btnAddActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ListAdd;
    private javax.swing.JList ListView;
    private javax.swing.JComboBox NumberCombo;
    private javax.swing.JComboBox PrefixCombo;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnDrop;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnExit2;
    private javax.swing.JButton btnMore;
    private javax.swing.JButton btnPrint;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

    public final void getPrefixes(){
        coursePrefixes = new String[1];
        coursePrefixes [0] = " ";
        System.out.print("get prefixes");
        try {
            System.out.print("trying");
            java.sql.ResultSet rs = parentThread.getConn().createStatement().executeQuery("SELECT course.type_ FROM section JOIN course ON section.course_id = course.id ORDER BY course.type_;");
            while (rs.next()){
                System.out.println("rs.next()");
                try{if (!coursePrefixes[coursePrefixes.length - 1].equals(rs.getString(1).toUpperCase())){
                    System.out.println("if Okay");
                    String [] temp = new String[coursePrefixes.length];
                    System.out.println("temp");
                    temp = coursePrefixes;
                    System.out.println("end of temp");
                    coursePrefixes = new String [temp.length + 1];
                    for(int j = 0; j < temp.length; j ++){
                        System.out.println(temp[j]);
                        coursePrefixes[j] = temp[j];
                    }
                    System.out.println("prefixes");
                    coursePrefixes [coursePrefixes.length - 1] = rs.getString(1).toUpperCase();
                    for(int j = 0; j < temp.length; j ++){System.out.println(coursePrefixes[j]);}
                    System.out.println("end of prefixes");
                    }
                }
                catch(Exception ex){
                    coursePrefixes = new String[1];
                    coursePrefixes[0] = rs.getString(1).toUpperCase();
                    System.out.println(coursePrefixes[0]);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(StudentPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void getNumbers(){
        courseNumber = new Object[1];
        courseNumber[0] = " ";
        System.out.print("get prefixes");
        try {
            System.out.print("trying");
            java.sql.ResultSet rs = parentThread.getConn().createStatement().executeQuery("SELECT course.number FROM section JOIN course ON section.course_id = course.id WHERE "
                    + "course.type_ LIKE \"" + coursePrefixes[PrefixCombo.getSelectedIndex()] + "\";");
            while (rs.next()){
                System.out.print("rs.next()");
                try{
                    Object [] temp;
                    temp = courseNumber;
                    courseNumber = new Object [temp.length + 1];
                    for (int j = 0; j < temp.length; j ++){
                        courseNumber[j] = temp[j];
                    }
                    courseNumber [courseNumber.length - 1] = new Integer(rs.getInt(1));
                    System.out.println(courseNumber[temp.length]);
                }
                catch(Exception ex){
                    coursePrefixes = new String[1];
                    coursePrefixes[0] = rs.getString(1);
                    System.out.println(coursePrefixes[0]);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(StudentPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void getSections(){
        model  = new javax.swing.DefaultListModel();
        int i= 0;
        int size;
        try{
            System.out.println(addSQL);
            sectionRS = parentThread.getConn().createStatement().executeQuery(addSQL);
            sectionRS.first();
            System.out.println(sqlEnding);
            java.sql.ResultSet temprs = parentThread.getConn().createStatement().executeQuery("SELECT count(*) FROM section JOIN course ON section.course_id = course.id " + sqlEnding);
            temprs.first();
            size = temprs.getInt(1);
            System.out.print(size);
            sectionRS.beforeFirst();
            addsections = new Object [3][size];
            sectionaddid = new int [size];
            while(sectionRS.next()){
                System.out.println(sectionRS.getString(1));
                addsections [0][i] = sectionRS.getString(1).toUpperCase();
                addsections [1][i] = new Integer(sectionRS.getInt(2));
                addsections [2][i] = new Integer(sectionRS.getInt(3));
                sectionaddid[i] = sectionRS.getInt(4);
                System.out.print(addsections[0][i] + " ");
                System.out.print(addsections[1][i] + " ");
                System.out.println(addsections[2][i]);
                i++;
            }
        }
        catch(Exception ex){
            System.out.print(ex.getCause());
            size = 1;
            addsections = new Object[3][1];
            addsections [0][0] = addsections [1][0] = addsections [2][0] = " ";
        }
        for (i = 0; i < size; i ++){
            try{model.add(i, ((String) addsections[0][i] + " " + (Integer) (addsections[1][i]) + " Section " + (Integer) (addsections[2][i])));}
            catch(Exception ex){
                model.add(i, ((String) addsections[0][i] + " " + (String) (addsections[1][i]) + " Section " + (String) (addsections[2][i])));
            }
        }

        ListAdd.setModel(model);
    }

    private void fillDropList(){
        javax.swing.DefaultListModel dropModel = new javax.swing.DefaultListModel();
        try{
            System.out.println("SELECT count(*) FROM student_section WHERE student_id = " + parentThread.getID() + ";");
            java.sql.ResultSet temprs = parentThread.getConn().createStatement().executeQuery("SELECT count(*) FROM student_section WHERE student_id = " + parentThread.getID() + ";");
            temprs.first();
            int size = temprs.getInt(1);
            System.out.println(size);
            java.sql.ResultSet rs = parentThread.getConn().createStatement().executeQuery(addSQL);

            dropsections = new Object[3][size];
            sectiondropid = new int [size];
            for(int j = 0; j < size; j ++){
                if (rs.next()){
                    dropsections[0][j] = rs.getString(1);
                    dropsections[1][j] = rs.getInt(2);
                    dropsections[2][j] = rs.getInt(3);
                    sectiondropid[j] = rs.getInt(4);
                    System.out.println("drop id:" + sectiondropid[j]);
                }
            }
            for(int i = 0; i < sectiondropid.length;i ++){
                System.out.println("index " + i + ": " + ((String)dropsections[0][i]).toUpperCase() + " " + (Integer)dropsections[1][i] + " " + (Integer)dropsections[2][i]);
                dropModel.add(i, (((String)dropsections[0][i]).toUpperCase() + " " + (Integer)dropsections[1][i] + " " + (Integer)dropsections[2][i]));
            }
        }
        catch(Exception ex){
            System.out.println(ex.getCause());
            sectiondropid = new int [] {0};
            dropModel.add(0, " ");
        }
        ListView.setModel(dropModel);
    }

    private void makeDropSQL(){
        dropSQL = "SELECT course.type_,course.number,section.section_id,section.id FROM"
                + " course JOIN section ON section.course_id = course.id JOIN student_section"
                + " ON student_section.section_id = section.id WHERE student_section.student_id = " + parentThread.getID() + ";";
    }

    private void makeAddSQL(int filledFields){
        sqlEnding = new String();
        addSQL = "SELECT course.type_,course.number,section.section_id,section.id FROM course JOIN section ON section.course_id = course.id";
        if (filledFields >= 1){
            sqlEnding = "WHERE (course.type_ LIKE \"" + ((String) PrefixCombo.getSelectedItem()).toLowerCase() + "\" OR course.type_ LIKE \"" + ((String) PrefixCombo.getSelectedItem()).toUpperCase() + "\")";
            addSQL += " WHERE (course.type_ LIKE \"" + ((String) PrefixCombo.getSelectedItem()).toLowerCase() + "\" OR course.type_ LIKE \"" + ((String) PrefixCombo.getSelectedItem()).toUpperCase() + "\")";
        }
        if (filledFields == 2) {
            sqlEnding += "AND course.number = " + NumberCombo.getSelectedItem();
            addSQL += " AND course.number = " + NumberCombo.getSelectedItem();
        }
        addSQL += ";";
    }

    private void addClass(){
        try{
        parentThread.getConn().createStatement().executeUpdate("INSERT INTO student_section (section_id,student_id) VALUES (" + sectionaddid[ListAdd.getSelectedIndex()] + "," + parentThread.getID() + ");");

        }
        catch (Exception ex){
            System.out.println(ex.getCause());
        }
    }

    String addSQL;
    String dropSQL;
    String sqlEnding;

    java.sql.ResultSet sectionRS;

    String [] coursePrefixes;
    Object [] courseNumber;
    Object [][] addsections;
    Object [][] dropsections;
    int [] sectiondropid;
    int [] sectionaddid;
    MainThread parentThread;

    javax.swing.DefaultListModel model;
}
